package org.jeecg.modules.product.basics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import dm.jdbc.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.ComboModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.product.basics.entity.TbUserAttestationConfig;
import org.jeecg.modules.product.basics.mapper.TbUserAttestationConfigMapper;
import org.jeecg.modules.product.basics.service.ITbUserAttestationConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 用户认证配置
 * @Author: jeecg-boot
 * @Date:   2025-02-21
 * @Version: V1.0
 */
@Slf4j
@Service
public class TbUserAttestationConfigServiceImpl extends ServiceImpl<TbUserAttestationConfigMapper, TbUserAttestationConfig> implements ITbUserAttestationConfigService {

    @Autowired
    private TbUserAttestationConfigMapper tbUserAttestationConfigMapper;
    @Autowired
    private ISysBaseAPI iSysBaseAPI;

    @Override
    public void sysUserSynchronization() {
        log.info("===============用户同步开始===============");
        int count = 0;
        List<LoginUser> sysUserList = iSysBaseAPI.queryAllSysUser("1","0");
        QueryWrapper<TbUserAttestationConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted","0");
        List<TbUserAttestationConfig> list = tbUserAttestationConfigMapper.selectList(queryWrapper);
        for (LoginUser sysUser : sysUserList) {
            if (!list.stream().anyMatch(item -> item.getUserName().equals(sysUser.getUsername()))) {
                TbUserAttestationConfig one = new TbUserAttestationConfig();
                one.setUserName(sysUser.getUsername());
                one.setRealName(sysUser.getRealname());
                one.setPassword(sysUser.getPassword());
                one.setDeleted("0");
                tbUserAttestationConfigMapper.insert(one);
                count++;
            }
        }
        log.info("===============用户同步完成==============="+count);
    }
}
